<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/05 13:40
 * Desc: 产品收藏相关处理
 */
class Controller_Pc_Api_Standard_Collect extends Controller_Pc_Api_Base
{

    /**
     * @function  检查产品是否已收藏
     */
    public function action_is_collect()
    {
        $mid = intval($this->request_body->content->mid);
        $type_id = intval($this->request_body->content->type_id);
        $id = intval($this->request_body->content->id);
        if(!$mid || !$type_id || !$id)
        {
            $result = array(
                "status"=> 0,
                "msg"=> '提交参数有误'
            );
        }
        else
        {
            $r = DB::select()->from('product_collect')->where('member_id', '=', $mid)->and_where('type_id', '=',$type_id)->and_where('auto_id', '=', $id)->execute()->current();
            $result = array(
                "status"=> 1,
                "is_collect"=> $r ? true : false,
                "msg"=> '获取成功'
            );
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }


    /**
     * @function 更改收藏
     */
    public function action_modify()
    {
        $mid = intval($this->request_body->content->mid);
        $type_id = intval($this->request_body->content->type_id);
        $id = intval($this->request_body->content->id);

        if(!$mid || !$type_id || !$id)
        {
            $result = array(
                "status"=> 0,
                "msg"=> '提交参数有误'
            );
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }

        // 开始收藏&取消操作
        $r = DB::select()->from('product_collect')->where('member_id', '=', $mid)->and_where('type_id', '=', $type_id)->and_where('auto_id', '=',$id)->execute()->current();
        $result = array(
            "status"=> 1
        );
        if ($r) {
            DB::delete('product_collect')->where('id', '=', $r['id'])->execute();
            $result['msg'] = "取消成功";
            $result['is_collect'] = false;
        } else {
            $i = array(
                'type_id' => $type_id,
                'auto_id' => $id,
                'member_id' => $mid
            );
            DB::insert('product_collect', array_keys($i))->values(array_values($i))->execute();
            $result['msg'] = "收藏成功";
            $result['is_collect'] = true;
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //会员中心获取收藏
    public function action_get_collect_list()
    {
        $mid = intval($this->request_body->content->mid);
        $pagesize = intval($this->request_body->content->pagesize);
        $startSize = intval($this->request_body->content->startSize);

        if(!$mid)
        {
            $result = array(
                "status"=> 0,
                "msg"=> '请先登录'
            );
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }

        $result = array('list' => array());
        $start = $startSize ? $startSize : 0;

        $result['list'] = DB::select()->from('product_collect')->where('member_id', '=', $mid)->offset($start)->order_by("id","DESC")->limit($pagesize)->execute()->as_array();

        $models = array();
        $res = DB::select()->from('model')->where('isopen', '=', 1)->execute();
        foreach ($res as $item) {
            $models[$item['id']] = $item;
        }

        foreach ($result['list'] as &$item) {
            $m = $models[$item['type_id']]['maintable'];
            $d = DB::select()->from($m)->where('id', '=', $item['auto_id'])->execute()->current();

            if ($m != 'guide_service') {
                $_model = "Model_" . ucfirst($m);
                $reflectionMethod = new ReflectionMethod($_model, 'get_minprice');
                $item['price'] = $reflectionMethod->invoke(new $_model(), $d['id'], array('info' => $d));
            }
            else
            {
                $item['price'] = $d['price'];
            }

            if ($item['type_id'] == 5) {
                $item['price'] = $item['price']['price'];
            }

            // 获取详情页面地址
            $item['url'] = Model_Api_Standard_System::get_product_detail_path($item['type_id']);
            $item['url'] = $item['url'] ? $item['url']."?id=".$item['auto_id'] : "";
            $item['commentnum'] = Model_Comment::get_comment_num($item['auto_id'], $item['type_id']);
            $item['satisfyscore'] = St_Functions::get_satisfy($item['type_id'], $d['id'], $d['satisfyscore']);
            $item['title'] = $d['title'];
            $item['litpic'] = Model_Api_Standard_System::reorganized_resource_url($d['litpic']);
            $item['model'] = $models[$item['type_id']]['modulename'];
        }

        $result['completed'] = count($result['list']) == $pagesize ? false : true;
        $result['status'] = true;

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 获取我的收藏数量
    public function action_get_collect_num()
    {
        $mid = intval($this->request_body->content->mid);
        if(!$mid)
        {
            $this->send_datagrams($this->client_info['id'], array("status"=> false ,"msg"=> '请先登录'), $this->client_info['secret_key']);
        }
        $num = DB::select(DB::expr("count(id) as num"))->from('product_collect')->where('member_id', '=', $mid)->execute()->get("num");
        $result = array("status"=> true, "msg"=> '获取成功', 'num'=> $num);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 删除收藏
    public function action_del_collect_product()
    {
        $mid = intval($this->request_body->content->mid);
        $ids = Common::remove_xss($this->request_body->content->ids);
        if(!$mid || !$ids)
        {
            $this->send_datagrams($this->client_info['id'], array("status"=> false ,"msg"=> '提交信息有误'), $this->client_info['secret_key']);
        }

        // 删除收藏
        $idArr = explode(',',$ids);
        DB::delete('product_collect')->where('member_id', '=', $mid)->and_where('id',"in",$idArr)->execute();
        $result = array("status"=> true, "msg"=> '删除成功');
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

}